package com.start.ad.admin;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.ad.entity.StartAdCategoryEntity;
import com.start.ad.service.StartAdCategoryService;
import com.start.common.api.Result;
import com.start.common.dataImport.DataImportEntity;
import com.start.common.dataImport.ImportExportUtils;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.security.annotations.DataPermission;
import com.start.security.annotations.IgnoreAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/start/ad/category")
@RegisterMenu(value = "广告栏目管理", parentId = "", systemCode = "start:ad:category", component = "views/start/ad/category/index", path = "/start/ad/category")
public class StartAdCategoryController {
    private static final String LOG_TITLE = "广告栏目管理";

    @Autowired
    private StartAdCategoryService service;

    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("views/start/ad/category/index")
    @PreAuthorize("@pas.hasPermission('start:ad:category:info')")
    public Result<?> list(Page<StartAdCategoryEntity> page, StartAdCategoryEntity entity) {
        QueryWrapper<StartAdCategoryEntity> queryWrapper = DataQueryConstructor.createEntity(StartAdCategoryEntity.class);
        return Result.ok(service.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('start:ad:category:info')")
    @RegisterPermission(value = "start:ad:category:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(service.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:ad:category:add')")
    @RegisterPermission(value = "start:ad:category:add", name = "添加")
    public Result<?> save(@RequestBody StartAdCategoryEntity entity) {
        service.addCategory(entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:ad:category:edit')")
    @RegisterPermission(value = "start:ad:category:edit", name = "修改")
    public Result<?> update(@RequestBody StartAdCategoryEntity entity) {
        service.updateCategoryById(entity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:ad:category:delete')")
    @RegisterPermission(value = "start:ad:category:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        service.deleteCategoryId(id);
        return Result.ok();
    }

    @GetMapping("/downloadTemplate")
    @DataPermission("physical/medicalCore/list")
    @PreAuthorize("@pas.hasPermission('start:ad:category:downloadTemplate')")
    @RegisterPermission(value = "start:ad:category:downloadTemplate", name = "下载导入模板")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "标签导入模板.xls";
        try {
            ImportExportUtils.createImportTemplate(StartAdCategoryEntity.class, response, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportExcel")
    @PreAuthorize("@pas.hasPermission('start:ad:category:exportExcel')")
    @RegisterPermission(value = "start:ad:category:exportExcel", name = "导出excel")
    @DataPermission("views/zm/common/tag/index")
    public void exportExcel(HttpServletResponse response) {
        String fileName = LOG_TITLE + "导出.xls";
        QueryWrapper<StartAdCategoryEntity> queryWrapper = DataQueryConstructor.createEntity(StartAdCategoryEntity.class);
        List<StartAdCategoryEntity> list = service.list(queryWrapper);
        ImportExportUtils.exportExcel(StartAdCategoryEntity.class, list, response, fileName);
    }


    @PostMapping("/importExcel")
    @PreAuthorize("@pas.hasPermission('start:ad:category:importExcel')")
    @RegisterPermission(value = "start:ad:category:importExcel", name = "导入excel")
    public Result<?> importExcel(@RequestBody DataImportEntity entity) {
        List<String> filePaths = entity.getFilePath();
        if (filePaths == null || filePaths.size() < 1) return Result.ok();
        List<StartAdCategoryEntity> dateList = ImportExportUtils.importExcelFile(StartAdCategoryEntity.class, filePaths.get(0));
        if (dateList.size() < 1) {
            return Result.ok();
        }
        service.saveBatch(dateList, 30);
        log.info("文件导入:" + JSON.toJSONString(dateList));
        return Result.ok();
    }

    @IgnoreAuth
    @GetMapping({"/getCategoryLisByCode"})
    public Result<?> getCategoryLisByCode(String code, boolean isTree) {
        return Result.ok(service.getCategoryLisByCode(code, isTree));
    }


}
